package com.bank.accountinfo.account.controller;

import com.bank.accountinfo.account.entity.Account;
import com.bank.accountinfo.account.service.AccountService;
import com.bank.setting.standard.result.Result;
import com.bank.trade_records.entity.TradeRecords;
import com.bank.trade_records.service.TradeRecordsService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin(origins = "http://localhost:8081") // 允许来自该源的请求
@RequestMapping("/api/account")
public class AccountController {

    @Autowired
    private AccountService accountService;
    @Autowired
    private TradeRecordsService tradeRecordsService;

    // 获取单个用户
    @GetMapping("/{id}")
    public Account getAccountById(@PathVariable Long id) {
        return accountService.getAccountById(id);
    }

    // 添加用户
    @PostMapping
    public boolean addAccount(@RequestBody Account account) {
        return accountService.addAccount(account);
    }

    // 更新用户
    @PutMapping("/updateById")
    public boolean updateAccount(@RequestBody Account account) {

        return accountService.updateAccount(account);
    }

    // 删除用户
    @DeleteMapping("/{id}")
    public boolean deleteAccountById(@PathVariable Long id) {
        return accountService.deleteAccountById(id);
    }

    // 获取所有用户
    @GetMapping
    public List<Account> getAllAccounts() {
        return accountService.getAllAccounts();
    }

    // 批量插入用户
    @PostMapping("/batch")
    public boolean batchInsert(@RequestBody List<Account> accountList) {
        return accountService.batchInsert(accountList);
    }

    @GetMapping("/page")
    public Page<Account> getAccountPage(@RequestParam(value = "page", defaultValue = "1") int page,
                                  @RequestParam(value = "size", defaultValue = "10") int size) {
        Page<Account> accountPage = new Page<>(page, size);
        return accountService.getAccountPage(accountPage);
    }
    //存款取款转账占比
    @PostMapping("/sum")
    public Result<List<List<Object>>> getTransactionSummary(){
        List<List<Object>> data=accountService.getTransactionSummary();
        return Result.success(data);
    }
    //当前账户交易记录账单
    @GetMapping("/account_sum/{accountId}")
    List<TradeRecords> getTransactionSummary(@PathVariable Long accountId){
        return tradeRecordsService.getTransactionSummary(accountId);
    }

    //待测试,数据库无数据
    @GetMapping("/{accountId}/balance-history")
    public List<List<Object>> getBalanceHistory(@PathVariable String accountId) {
        return accountService.getBalanceListByAccountId(accountId);
    }

    // 其他接口...
}
